@extends('admin.layouts.layout')
@section('content')
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox-title">
                <h5>添加邮件</h5>
            </div>
            <div class="ibox-content">
                <a class="menuid btn btn-primary btn-sm m-b-sm" href="{{getBackUrl()}}">返回</a>
                <form id="myForm" class="form-horizontal" action="{{route('mail.store')}}" method="post"
                      enctype="multipart/form-data">
                    {!! csrf_field() !!}
                    <input type="hidden" name="goBackUrl" value="{{getBackUrl()}}"/>
                    <table class="table-striped table-bordered table-hover table-condensed">
                        <tr class="b-group">
                            <th style="min-width: 108px;">
                                <label>渠道</label>
                            </th>
                            <td class="b-child">
                                <div style="max-height:6.5rem;overflow-y:scroll;">
                                    @include('widget.channel_radio')
                                </div>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>邮件类型</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td id="tdChannels">
                                            <?php
                                            use \App\Models\Enum\MailType;
                                            $mailType = [
                                                MailType::FULL_SERVICE => MailType::get(MailType::FULL_SERVICE),
                                                MailType::SINGLE_SERVICE => MailType::get(MailType::SINGLE_SERVICE),
                                                MailType::SINGLE => MailType::get(MailType::SINGLE),
                                            ];
                                            $mailTypeHtml = '';
                                            $show = 'none';
                                            foreach ($mailType as $key => $type) {
                                                $check = ' ';
                                                if (old('mail_type') == $key) {
                                                    $check = ' checked="checked" ';
                                                }

                                                if (old('mail_type') == MailType::SINGLE) {
                                                    $show = 'block';
                                                }

                                                $mailTypeHtml .= <<<EOF
 <div class="col-sm-2" style="min-width: 20rem;">
     <label>&emsp;$type
            <input class="pull-left" type="radio" name="mail_type" value="$key" $check>
    </label>
</div>
EOF;
                                            }
                                            echo $mailTypeHtml;
                                            ?>
                                            <input class="pull-left" style="display: {{$show}};" type="text"
                                                   name="nickname"
                                                   value="{{old('nickname','')}}"
                                                   placeholder="请输入玩家昵称" autocomplete="off">
                                        </td>
                                        <?php
                                        if ($errors->has('mail_type')) {
                                            echo "<td><span class=\"help-block m-t-none\"><i class=\"fa fa-info-circle\"></i><code>{$errors->first('mail_type')}</code></span></td>";
                                        }
                                        if ($errors->has('nickname')) {
                                            echo "<td><span class=\"help-block m-t-none\"><i class=\"fa fa-info-circle\"></i><code>{$errors->first('nickname')}</code></span></td>";
                                        }
                                        ?>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>服务器</label>
                            </th>
                            <td class="b-child">
                                <div style="max-height:10rem;overflow-y:scroll;">
                                    @include('widget.server_radio')
                                </div>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>邮件标题</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td id="tdChannels">
                                            <input class="pull-left col-xs-12" type="text" name="title"
                                                   value="{{old('title','')}}"
                                                   placeholder="请输入邮件标题" maxlength="20" autocomplete="off">
                                        </td>
                                        <?php
                                        if ($errors->has('title')) {
                                            echo "<td><span class=\"help-block m-t-none\"><i class=\"fa fa-info-circle\"></i><code>{$errors->first('title')}</code></span></td>";
                                        }
                                        ?>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>邮件内容</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td id="tdChannels">
                                            <input class="pull-left col-xs-12" type="text" name="content"
                                                   value="{{old('content','')}}"
                                                   placeholder="请输入邮件内容" maxlength="200" autocomplete="off">
                                        </td>
                                        <?php
                                        if ($errors->has('content')) {
                                            echo "<td><span class=\"help-block m-t-none\"><i class=\"fa fa-info-circle\"></i><code>{$errors->first('content')}</code></span></td>";
                                        }
                                        ?>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>发放理由</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td id="tdChannels">
                                            <input class="pull-left col-xs-12" type="text" name="reason"
                                                   value="{{old('reason','')}}"
                                                   placeholder="请输入发放理由" maxlength="200" autocomplete="off">
                                        </td>
                                        <?php
                                        if ($errors->has('reason')) {
                                            echo "<td><span class=\"help-block m-t-none\"><i class=\"fa fa-info-circle\"></i><code>{$errors->first('reason')}</code></span></td>";
                                        }
                                        ?>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>附件1</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td class="form-inline">
                                            <select data-placeholder="选择物品" class="chosen-select" name="item_id_1">
                                                <option value="">选择物品</option>
                                                <?php
                                                $items_html = '';
                                                $selected = '';
                                                foreach ($items as $item) {

                                                    if ($item->item_key == old('item_id_1')) {
                                                        $selected = 'selected';
                                                    } else {
                                                        $selected = '';
                                                    }

                                                    $items_html .= "<option value=\"{$item->item_key}\" hassubinfo=\"true\" {$selected}>{$item->item_name}({$item->item_key})</option>";
                                                }

                                                echo $items_html;
                                                ?>
                                            </select>
                                            <div class="input-group">
                                                <span class="input-group-addon">X</span>
                                                <input type="text" placeholder="数量" name="item_num_1"
                                                       value="{{old('item_num_1','')}}"
                                                       class="form-control" autocomplete="off">
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>附件2</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td class="form-inline">
                                            <select data-placeholder="选择物品" class="chosen-select" name="item_id_2">
                                                <option value="">选择物品</option>
                                                <?php
                                                $items_html = '';
                                                $selected = '';
                                                foreach ($items as $item) {

                                                    if ($item->item_key == old('item_id_2')) {
                                                        $selected = 'selected';
                                                    } else {
                                                        $selected = '';
                                                    }

                                                    $items_html .= "<option value=\"{$item->item_key}\" hassubinfo=\"true\" {$selected}>{$item->item_name}({$item->item_key})</option>";
                                                }

                                                echo $items_html;
                                                ?>
                                            </select>
                                            <div class="input-group">
                                                <span class="input-group-addon">X</span>
                                                <input type="text" placeholder="数量" name="item_num_2"
                                                       value="{{old('item_num_2','')}}"
                                                       class="form-control" autocomplete="off">
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>附件3</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td class="form-inline">
                                            <select data-placeholder="选择物品" class="chosen-select" name="item_id_3">
                                                <option value="">选择物品</option>
                                                <?php
                                                $items_html = '';
                                                $selected = '';
                                                foreach ($items as $item) {

                                                    if ($item->item_key == old('item_id_3')) {
                                                        $selected = 'selected';
                                                    } else {
                                                        $selected = '';
                                                    }

                                                    $items_html .= "<option value=\"{$item->item_key}\" hassubinfo=\"true\" {$selected}>{$item->item_name}({$item->item_key})</option>";
                                                }

                                                echo $items_html;
                                                ?>
                                            </select>
                                            <div class="input-group">
                                                <span class="input-group-addon">X</span>
                                                <input type="text" placeholder="数量" name="item_num_3"
                                                       value="{{old('item_num_3','')}}"
                                                       class="form-control" autocomplete="off">
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>附件4</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td class="form-inline">
                                            <select data-placeholder="选择物品" class="chosen-select" name="item_id_4">
                                                <option value="">选择物品</option>
                                                <?php
                                                $items_html = '';
                                                $selected = '';
                                                foreach ($items as $item) {

                                                    if ($item->item_key == old('item_id_4')) {
                                                        $selected = 'selected';
                                                    } else {
                                                        $selected = '';
                                                    }

                                                    $items_html .= "<option value=\"{$item->item_key}\" hassubinfo=\"true\" {$selected}>{$item->item_name}({$item->item_key})</option>";
                                                }

                                                echo $items_html;
                                                ?>
                                            </select>
                                            <div class="input-group">
                                                <span class="input-group-addon">X</span>
                                                <input type="text" placeholder="数量" name="item_num_4"
                                                       value="{{old('item_num_4','')}}"
                                                       class="form-control" autocomplete="off">
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                                <label>附件5</label>
                            </th>
                            <td class="b-child">
                                <table class="table-striped table-bordered table-hover table-condensed col-xs-12"
                                       style="margin: 0px;">
                                    <tr class="b-group">
                                        <td class="form-inline">
                                            <select data-placeholder="选择物品" class="chosen-select" name="item_id_5">
                                                <option value="">选择物品</option>
                                                <?php
                                                $items_html = '';
                                                $selected = '';
                                                foreach ($items as $item) {

                                                    if ($item->item_key == old('item_id_5')) {
                                                        $selected = 'selected';
                                                    } else {
                                                        $selected = '';
                                                    }

                                                    $items_html .= "<option value=\"{$item->item_key}\" hassubinfo=\"true\" {$selected}>{$item->item_name}({$item->item_key})</option>";
                                                }

                                                echo $items_html;
                                                ?>
                                            </select>
                                            <div class="input-group">
                                                <span class="input-group-addon">X</span>
                                                <input type="text" placeholder="数量" name="item_num_5"
                                                       value="{{old('item_num_5','')}}"
                                                       class="form-control" autocomplete="off">
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th width="10%">
                            </th>
                            <td class="b-child">
                                @include('widget.query_and_refresh',['submit_button_name'=>'提 交'])
                            </td>
                        </tr>
                    </table>
                    <input type="hidden" id="hidAttachment" value="" name="attachment"/>
                </form>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>
    <script>
    </script>
    <link href="{{loadEdition('/admin/css/plugins/datapicker/bootstrap-datepicker.css')}}" rel="stylesheet">
    <link href="{{loadEdition('/admin/js/plugins/bootstrap-table/bootstrap-table.min.css')}}" rel="stylesheet">
    <link href="{{loadEdition('/js/plugins/chosen_v1.8.7/chosen.min.css')}}" rel="stylesheet">

@endsection
@section('js')
    <!--bootstrap-datepicker start-->
    <script src="{{loadEdition('/admin/js/plugins/datapicker/bootstrap-datepicker.js')}}"></script>
    <script src="{{loadEdition('/admin/js/plugins/datapicker/bootstrap-datepicker.zh-CN.js')}}"></script>
    <!--bootstrap-datepicker end-->
    <!--bootstrap-table start-->
    <script src="{{loadEdition('/admin/js/plugins/bootstrap-table/bootstrap-table.min.js')}}"></script>
    <script src="{{loadEdition('/admin/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.js')}}"></script>
    <!--bootstrap-table end-->
    <!--chosen-->
    <script src="{{loadEdition('/js/plugins/chosen_v1.8.7/chosen.jquery.min.js')}}"></script>
    <script src="{{loadEdition('/js/plugins/chosen_v1.8.7/chosen.proto.min.js')}}"></script>
    <script src="{{loadEdition('/js/global.js')}}"></script>
    <script>
        $(document).ready(function () {
            //页面初始化
            if ($("input:checked[name='mail_type']").val() == '{{\App\Models\Enum\MailType::FULL_SERVICE}}') {
                $('input[name="server_id"]').prop('disabled', true);
            }

            //邮件类型  切换
            $('input[type="radio"][name="mail_type"]').change(function () {
                if ($(this).val() == '{{\App\Models\Enum\MailType::SINGLE}}') {
                    $('input[name="nickname"]').show();
                } else {
                    $('input[name="nickname"]').hide();
                }

                if ($(this).val() == '{{\App\Models\Enum\MailType::FULL_SERVICE}}') {
                    $('input[name="server_id"]').prop('disabled', true);
                } else {
                    $('input[name="server_id"]').prop('disabled', false);
                }
            });

            //把附件名称组合在一起
            $('#myForm').submit(function () {
                var attachment = '';
                $('.chosen-single span').each(function (index, element) {
                    var id = index + 1;
                    var item_num = $('input[name="item_num_' + id + '"]').val();
                    var item_name = element.innerHTML;

                    if (item_name != '' && item_num > 0) {
                        attachment += item_name + '(' + item_num + '),<br/>';
                    }
                })

                if (attachment.length > 0) {
                    attachment = attachment.substr(0, attachment.length - 1);
                }

                $('#hidAttachment').val(attachment);

                //对内容中的\n进行编码
                var content = $('input[name="content"]').val();
                content = content.replace(/\\n/, "%5Cn");
                $('input[name="content"]').val(content);

                return true;
            });

        });
    </script>
@stop